import {
  _decorator,
  CCInteger,
  Collider2D,
  Component,
  instantiate,
  math,
  Node,
  Prefab,
  Vec3,
  Contact2DType,
} from 'cc'
const { ccclass, property } = _decorator

@ccclass('RewardManager')
export class RewardManager extends Component {
  // 双发子弹奖励
  @property({ type: Prefab })
  twoShootPrefab: Prefab = null
  // 炸弹奖励
  @property({ type: Prefab })
  bombPrefab: Prefab = null
  // 奖励生成频率
  @property({ type: CCInteger })
  rewardSpawnRate: number = 20

  start() {
    this.schedule(this.rewardSpawn, this.rewardSpawnRate)
  }
  rewardSpawn() {
    const random = math.randomRangeInt(0, 2)
    this.prefabSpawn(random === 0 ? this.twoShootPrefab : this.bombPrefab, -210, 210, 470)
  }
  prefabSpawn(rewardPrefab: Prefab, minX: number, maxX: number, Y: number) {
    const reward = instantiate(rewardPrefab)
    this.node.addChild(reward)
    reward.setPosition(new Vec3(math.randomRangeInt(minX, maxX), Y, 0))
  }
  protected onDestroy(): void {
    this.unschedule(this.prefabSpawn)
  }
}
